package com.bilibili.juc.cf.base;

import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
 
public class CompletableFutureAdapter<T> extends AbstractFuture<T> {
    private final CompletableFuture<T> completableFuture;
 
    public CompletableFutureAdapter(CompletableFuture<T> completableFuture) {
        this.completableFuture = completableFuture;
        completableFuture.whenComplete((result, throwable) -> {
            if (throwable != null) {
                setException(throwable);
            } else {
                set(result);
            }
        });
    }
 
    public static <T> ListenableFuture<T> adapt(CompletableFuture<T> future) {
        return new CompletableFutureAdapter<>(future);
    }
}